package com.appleframework.module.usermgr.dao;

import java.util.List;
import java.util.Map;

import java.sql.Types;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.appleframework.core.BaseDao;
import com.appleframework.core.Page;
import com.appleframework.core.ServiceException;
import com.appleframework.module.usermgr.vo.Item;
import com.appleframework.module.usermgr.vo.Itemdetail;
import com.appleframework.module.usermgr.vo.SUser;



@Repository("itemMgrDao")
public class ItemMgrDao extends BaseDao {
	
	
	//根据字段查询
	public List<Item>  getItemsByColunm(String columnName, Object value) {
		
		return getJdbcTemplate().query("select *  from t_item_detail where "+columnName+"=?", new BeanPropertyRowMapper(Item.class),value);
	}
	
	//查询所有item
	public List<Item> getItems() {
		System.out.println("getItems");
		return getJdbcTemplate().query("select *  from t_item",BeanPropertyRowMapper.newInstance(Item.class));
	}
	
	/**
	 * 分页查询-根据name/所有，进行分页显示
	 * @param start 记录开始
	 * @param pageSize 每页显示的记录数
	 * @param name 名称
	 * @return
	 * @throws ServiceException
	 */
	public List<Item> getPage(Integer start, Integer pageSize,String name){
		
		//String sql="select * from t_item ";
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_item");
		if(null != name && !"".equals(name)){
			sb.append(" where name like "); 
			sb.append("'%");
			sb.append(name);
			sb.append("%'");
		}
		sb.append(" limit ");
		sb.append(start);
		sb.append(",");
		sb.append(pageSize);
		
		System.out.println(sb.toString());
		return getJdbcTemplate().query(sb.toString(),BeanPropertyRowMapper.newInstance(Item.class) );
		
		
		
	}
	
	//添加
	
	public void addItem(Item item){
		
		String sql="insert into t_item (name,descript,itemcode,createtime,createuserid) values('"
			+item.getName()+"','"+item.getDescript()+"','"+item.getItemcode()+"',now(),"+item.getCreateuserid()+")";
		System.out.println(sql);
		
		getJdbcTemplate().update(sql);
	}
	
	
	
	//删除
	public void deleteItem(String columnName, Object value){
		String sql="delete from t_item where "+columnName+"="+value;
		System.out.println(sql);
		getJdbcTemplate().update(sql);
		
	}
	//修改
	public void updateItem(Item item){
		System.out.println(item.getDescript());
		
		String sql="update t_item set name='"+item.getName()+"',descript='"+item.getDescript()+"',itemcode='"+item.getItemcode()+"',updatetime=now() where id="+item.getId();
		System.out.println(sql);

		getJdbcTemplate().update(sql);
		
	}
	
	/**
	 * 查询一共多少条记录  
	 * 
	 * @return 总共多少条记录
	 */
	public int findRecordCount(){
		return getJdbcTemplate().queryForInt("select * from t_item");
	};
	

}
